<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>模拟退火详解 AND P1433题解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一个极度玄学的算法。">
<meta property="og:type" content="article">
<meta property="og:title" content="模拟退火详解 AND P1433题解">
<meta property="og:url" content="http://example.com/2020/08/26/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E8%AF%A6%E8%A7%A3%20AND%E3%80%8CP1433%E3%80%8D%E9%A2%98%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="一个极度玄学的算法。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-26T09:44:03.567Z">
<meta property="article:modified_time" content="2023-07-19T11:50:24.598Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-模拟退火详解 AND「P1433」题解" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/08/26/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E8%AF%A6%E8%A7%A3%20AND%E3%80%8CP1433%E3%80%8D%E9%A2%98%E8%A7%A3/" class="article-date">
  <time class="dt-published" datetime="2020-08-26T09:44:03.567Z" itemprop="datePublished">2020-08-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      模拟退火详解 AND P1433题解
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>一个极度玄学的算法。</p>
<p>前排提示：LZ是个菜比，有可能有讲的不对的地方，请在评论区指出qwq</p>
<h1 id="0-基本思想"><a href="#0-基本思想" class="headerlink" title="0.基本思想"></a>0.基本思想</h1><p>模拟退火其实没有那么高大上。说白了就是<strong>初始化一个“温度”。每次随机乱选一个方案，如果比以前的方案优那么就要，否则就以一定的概率要或者不要。当前方案越狗屎就越不想要，“温度”越低越不想要。然后把温度降低一些，反复循环，直到温度为0为止。</strong></p>
<h1 id="1-照本宣科-实现"><a href="#1-照本宣科-实现" class="headerlink" title="1.照本宣科 实现"></a>1.<del>照本宣科</del> 实现</h1><p>呃，就以 <del>臭名昭著</del> 著名的TSP问题举例子吧。</p>
<p>什么？你不知道TSP？这个就是-&gt;<a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1433">点我</a>。</p>
<p>其实正解是搜索，但是$O(n!)$的时间复杂度实在伤不起（除了像本题一样$n\le15$），所以考虑模拟退火。</p>
<p>首先，初始化一个初始”温度“。<strong>越高越好，但是过高会让程序变慢，至于为什么以后再说</strong>。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">double</span> T0=<span class="number">1e5</span><span class="comment">/*初始温度*/</span>,T_end=<span class="number">1e-4</span><span class="comment">/*结束温度（由于非常接近0可以看作0）*/</span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SA</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> T=T0;<span class="comment">//当前温度</span></span><br><span class="line">    <span class="keyword">while</span>(T&gt;T_end)&#123;<span class="comment">//对应”反复循环，直到温度为0为止。“这句话</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后胡乱生成一个解：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">double</span> <span class="title">calc</span><span class="params">()</span></span>&#123;<span class="comment">//意思是查询当前解的代价，具体到问题里就是按照当前顺序访问要走多远</span></span><br><span class="line">    <span class="type">double</span> ret=<span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        ret+=g[ans[i<span class="number">-1</span>]][ans[i]];<span class="comment">//g数组的意思是从一个点到另一个点要走多少</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">random_disp</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;<span class="comment">//意思是在区间[l,r]内随机生成一个数</span></span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="keyword">if</span>(l&gt;r)<span class="built_in">swap</span>(l,r);</span><br><span class="line">    <span class="function">uniform_int_distribution&lt;<span class="type">int</span>&gt; <span class="title">u</span><span class="params">(l,r)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> T0=<span class="number">1e5</span><span class="comment">/*初始温度*/</span>,T_end=<span class="number">1e-4</span><span class="comment">/*结束温度（由于非常接近0可以看作0）*/</span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SA</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> T=T0;<span class="comment">//当前温度</span></span><br><span class="line">    <span class="keyword">while</span>(T&gt;T_end)&#123;<span class="comment">//对应”反复循环，直到温度为0为止。“这句话</span></span><br><span class="line">        <span class="type">int</span> u=<span class="built_in">random_disp</span>(<span class="number">1</span>,n),v=<span class="built_in">random_disp</span>(<span class="number">1</span>,n);</span><br><span class="line">        <span class="built_in">swap</span>(ans[u],ans[v]);</span><br><span class="line">        <span class="type">double</span> new_sol=<span class="built_in">calc</span>();<span class="comment">//随机交换两个数，就相当于乱生成一个</span></span><br><span class="line">        <span class="comment">//ans数组的意义是访问的顺序</span></span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>判断是否要这个解：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">inline</span> <span class="type">bool</span> <span class="title">CBP</span><span class="params">(<span class="type">double</span> x)</span></span>&#123;<span class="comment">//Choose by probability.</span></span><br><span class="line">                          <span class="comment">//以概率x返回 true或者false</span></span><br><span class="line">    <span class="keyword">if</span>(x&gt;=<span class="number">1.0</span>)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span>(x&lt;=<span class="number">0.0</span>)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="function">uniform_real_distribution&lt;<span class="type">double</span>&gt; <span class="title">u</span><span class="params">(<span class="number">0.0</span>,<span class="number">1.0</span>)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine)&lt;=x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">calc</span><span class="params">()</span></span>&#123;<span class="comment">//意思是查询当前解的代价，具体到问题里就是按照当前顺序访问要走多远</span></span><br><span class="line">    <span class="type">double</span> ret=<span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        ret+=g[ans[i<span class="number">-1</span>]][ans[i]];<span class="comment">//g数组的意思是从一个点到另一个点要走多少</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">random_disp</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;<span class="comment">//意思是在区间[l,r]内随机生成一个数</span></span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="keyword">if</span>(l&gt;r)<span class="built_in">swap</span>(l,r);</span><br><span class="line">    <span class="function">uniform_int_distribution&lt;<span class="type">int</span>&gt; <span class="title">u</span><span class="params">(l,r)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> T0=<span class="number">1e5</span><span class="comment">/*初始温度*/</span>,T_end=<span class="number">1e-4</span><span class="comment">/*结束温度（由于非常接近0可以看作0）*/</span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SA</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> T=T0;<span class="comment">//当前温度</span></span><br><span class="line">    <span class="keyword">while</span>(T&gt;T_end)&#123;<span class="comment">//对应”反复循环，直到温度为0为止。“这句话</span></span><br><span class="line">        <span class="type">int</span> u=<span class="built_in">random_disp</span>(<span class="number">1</span>,n),v=<span class="built_in">random_disp</span>(<span class="number">1</span>,n);</span><br><span class="line">        <span class="built_in">swap</span>(ans[u],ans[v]);</span><br><span class="line">        <span class="type">double</span> new_sol=<span class="built_in">calc</span>();<span class="comment">//随机交换两个数，就相当于乱生成一个</span></span><br><span class="line">        <span class="comment">//ans数组的意义是访问的顺序</span></span><br><span class="line">        <span class="keyword">if</span>(new_sol&lt;old_sol)&#123;<span class="comment">//如果撞到狗屎运，随机乱搞一个都比以前的解好</span></span><br><span class="line">            old_sol=new_sol;<span class="comment">//那么更新</span></span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">CBP</span>(<span class="built_in">exp</span>(<span class="built_in">double</span>(old_sol-new_sol)/T)))&#123;<span class="comment">//否则以概率决定是否更新</span></span><br><span class="line">            old_sol=new_sol;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">swap</span>(ans[u],ans[v]);<span class="comment">//换回来，交换两次等于没换</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>等等，<code>exp(double(old_sol-new_sol)/T)</code>是什么意思？</p>
<h6 id="这个我当初也蒙了半天（我太蔡了），尽量讲的明白一点"><a href="#这个我当初也蒙了半天（我太蔡了），尽量讲的明白一点" class="headerlink" title="这个我当初也蒙了半天（我太蔡了），尽量讲的明白一点"></a><em>这个我当初也蒙了半天（我太蔡了），尽量讲的明白一点</em></h6><p>先把它翻译成数学语言：</p>
<script type="math/tex; mode=display">e^{\frac{\Delta f}{T}}</script><p>再翻译成人话：</p>
<p>$e$ <em>(是个常数，大约是2.7)</em> 的 (以前解 - 当前解 )除以当前温度次方</p>
<p> (以前解 - 当前解 )，也就是$\Delta f$，一定是个负数，为什么看看代码就知道了。</p>
<p> 那么，<strong>$\Delta f$越小（也就是绝对值越大），也就是当前解越狗屎，$\frac{\Delta f}{T}$就越小。当$T$越小，也就是温度越小，$\frac{\Delta f}{T}$的绝对值也就越大，$\frac{\Delta f}{T}$也就越小。$\frac{\Delta f}{T}$越小，$e^{\frac{\Delta f}{T}}$也就越小（但一定大于0），正好对应了”当前方案越狗屎就越不想要，“温度”越低越不想要。“这句话。</strong></p>
<p> 降低温度并记录遇到的最优解：<br> <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">inline</span> <span class="type">bool</span> <span class="title">CBP</span><span class="params">(<span class="type">double</span> x)</span></span>&#123;<span class="comment">//Choose by probability.</span></span><br><span class="line">                          <span class="comment">//以概率x返回 true或者false</span></span><br><span class="line">    <span class="keyword">if</span>(x&gt;=<span class="number">1.0</span>)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span>(x&lt;=<span class="number">0.0</span>)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="function">uniform_real_distribution&lt;<span class="type">double</span>&gt; <span class="title">u</span><span class="params">(<span class="number">0.0</span>,<span class="number">1.0</span>)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine)&lt;=x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">calc</span><span class="params">()</span></span>&#123;<span class="comment">//意思是查询当前解的代价，具体到问题里就是按照当前顺序访问要走多远</span></span><br><span class="line">    <span class="type">double</span> ret=<span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        ret+=g[ans[i<span class="number">-1</span>]][ans[i]];<span class="comment">//g数组的意思是从一个点到另一个点要走多少</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">random_disp</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;<span class="comment">//意思是在区间[l,r]内随机生成一个数</span></span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="keyword">if</span>(l&gt;r)<span class="built_in">swap</span>(l,r);</span><br><span class="line">    <span class="function">uniform_int_distribution&lt;<span class="type">int</span>&gt; <span class="title">u</span><span class="params">(l,r)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine);</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> T0=<span class="number">1e5</span><span class="comment">/*初始温度*/</span>,T_end=<span class="number">1e-4</span><span class="comment">/*结束温度（由于非常接近0可以看作0）*/</span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SA</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> T=T0;<span class="comment">//当前温度</span></span><br><span class="line">    <span class="keyword">while</span>(T&gt;T_end)&#123;<span class="comment">//对应”反复循环，直到温度为0为止。“这句话</span></span><br><span class="line">        <span class="type">int</span> u=<span class="built_in">random_disp</span>(<span class="number">1</span>,n),v=<span class="built_in">random_disp</span>(<span class="number">1</span>,n);</span><br><span class="line">        <span class="built_in">swap</span>(ans[u],ans[v]);</span><br><span class="line">        <span class="type">double</span> new_sol=<span class="built_in">calc</span>();<span class="comment">//随机交换两个数，就相当于乱生成一个</span></span><br><span class="line">        <span class="comment">//ans数组的意义是访问的顺序</span></span><br><span class="line">        <span class="keyword">if</span>(new_sol&lt;old_sol)&#123;<span class="comment">//如果撞到狗屎运，随机乱搞一个都比以前的解好</span></span><br><span class="line">            old_sol=new_sol;<span class="comment">//那么更新</span></span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">CBP</span>(<span class="built_in">exp</span>(<span class="built_in">double</span>(old_sol-new_sol)/T)))&#123;<span class="comment">//否则以概率决定是否更新</span></span><br><span class="line">            old_sol=new_sol;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">swap</span>(ans[u],ans[v]);<span class="comment">//换回来，交换两次等于没换</span></span><br><span class="line">        &#125;</span><br><span class="line">        ans_val=<span class="built_in">min</span>(ans_val,old_sol);</span><br><span class="line">        ans_val=<span class="built_in">min</span>(ans_val,new_sol);<span class="comment">//记录最优解</span></span><br><span class="line">        T*=<span class="number">0.997</span>;<span class="comment">//缓缓降低</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br> 然后，不停循环，直到温度为0为止。</p>
<p> Code：</p>
 <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 20</span></span><br><span class="line"><span class="type">int</span> n,ans[MAXN],st=<span class="built_in">clock</span>();</span><br><span class="line"><span class="type">double</span> g[MAXN][MAXN],x[MAXN],y[MAXN],ans_val=<span class="number">1e10</span>;</span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">euc_dis</span><span class="params">(<span class="type">double</span> x1,<span class="type">double</span> y1,<span class="type">double</span> x2,<span class="type">double</span> y2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="type">bool</span> <span class="title">CBP</span><span class="params">(<span class="type">double</span> x)</span></span>&#123;<span class="comment">//Choose by probability</span></span><br><span class="line">    <span class="keyword">if</span>(x&gt;=<span class="number">1.0</span>)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span>(x&lt;=<span class="number">0.0</span>)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="function">uniform_real_distribution&lt;<span class="type">double</span>&gt; <span class="title">u</span><span class="params">(<span class="number">0.0</span>,<span class="number">1.0</span>)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine)&lt;=x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">random_disp</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;</span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="function"><span class="type">static</span> std::mt19937 <span class="title">random_engine</span><span class="params">(rand())</span></span>;</span><br><span class="line">    <span class="keyword">if</span>(l&gt;r)<span class="built_in">swap</span>(l,r);</span><br><span class="line">    <span class="function">uniform_int_distribution&lt;<span class="type">int</span>&gt; <span class="title">u</span><span class="params">(l,r)</span></span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">u</span>(random_engine);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">calc</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> ret=<span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        ret+=g[ans[i<span class="number">-1</span>]][ans[i]];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">const</span> <span class="type">double</span> T0=<span class="number">1e5</span>,T_end=<span class="number">1e-4</span>,DT=<span class="number">0.997</span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SA</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">double</span> T=T0,old_sol=<span class="built_in">calc</span>();</span><br><span class="line">    <span class="keyword">while</span>(T&gt;T_end)&#123;</span><br><span class="line">        <span class="type">int</span> u=<span class="built_in">random_disp</span>(<span class="number">1</span>,n),v=<span class="built_in">random_disp</span>(<span class="number">1</span>,n);</span><br><span class="line">        <span class="built_in">swap</span>(ans[u],ans[v]);</span><br><span class="line">        <span class="type">double</span> new_sol=<span class="built_in">calc</span>();</span><br><span class="line">        <span class="keyword">if</span>(new_sol&lt;old_sol)&#123;</span><br><span class="line">            old_sol=new_sol;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">CBP</span>(<span class="built_in">exp</span>(<span class="built_in">double</span>(old_sol-new_sol)/T)))&#123;</span><br><span class="line">            old_sol=new_sol;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">swap</span>(ans[u],ans[v]);</span><br><span class="line">        &#125;</span><br><span class="line">        ans_val=<span class="built_in">min</span>(ans_val,old_sol);</span><br><span class="line">        ans_val=<span class="built_in">min</span>(ans_val,new_sol);</span><br><span class="line">        T*=DT;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">srand</span>(<span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%lf %lf&quot;</span>,x+i,y+i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;=n;j++)&#123;</span><br><span class="line">            g[i][j]=<span class="built_in">euc_dis</span>(x[i],y[i],x[j],y[j]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        ans[i]=i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(<span class="built_in">clock</span>()-st&lt;<span class="number">0.95</span>*CLOCKS_PER_SEC)&#123;<span class="comment">//只要还没超时就不停退火</span></span><br><span class="line">        <span class="built_in">SA</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%.2lf\n&quot;</span>,ans_val);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> 以上代码能够AC<a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1433">P1433</a>，也就是例题。</p>
<h1 id="3-一些注意事项"><a href="#3-一些注意事项" class="headerlink" title="3.一些注意事项"></a>3.一些注意事项</h1><ol>
<li>由于模拟退火是个概率算法，所以除非你想不出正解最好不要用。</li>
<li>由于模拟退火是个概率算法，所以最好多跑几遍。</li>
<li>由于模拟退火是个概率算法，所以要仔细调整几个参数——初始温度、结束温度、变化率。</li>
<li>由于模拟退火是个概率算法，所以时间复杂度是 $O(玄学)$。初始温度越高，温度变化率越接近1，跑得越慢，也越精确。</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/08/26/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E8%AF%A6%E8%A7%A3%20AND%E3%80%8CP1433%E3%80%8D%E9%A2%98%E8%A7%A3/" data-id="clk9o8ln7000f8wi515o8e2w0" data-title="模拟退火详解 AND P1433题解" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/08/28/%E6%A0%91%E9%93%BE%E5%89%96%E5%88%86%E8%AF%A6%E8%A7%A3%20AND%20%E9%A2%98%E8%A7%A3%20P6098%20%E3%80%90%5BUSACO19FEB%5DCow%20Land%20G%E3%80%91/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          树链剖分详解 AND 题解 P6098 【[USACO19FEB]Cow Land G】
        
      </div>
    </a>
  
  
    <a href="/2020/08/24/%E9%A2%98%E8%A7%A3%20P2426%20%E3%80%90%E5%88%A0%E6%95%B0%E3%80%91/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">题解 P2426 【删数】</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>